package com.chao.demo.test;

import com.alibaba.fastjson.JSONObject;
import com.baiwang.open.client.login.BopLoginClient;
import com.baiwang.open.client.login.BopLoginResponse;
import com.baiwang.open.client.login.PasswordLoginClient;
import com.baiwang.open.client.login.PasswordLoginConfig;
import com.baiwang.open.exception.BWOpenException;

import java.util.UUID;

/**
 * 获取百望 token
 * @author baiwang
 * @date 2021-06-16
 */
public class Token {



    // 百望云开放平台沙箱环境地址
    private static String url = "http://openapi.baiwang.com/router/rest";
    // 百望云开放平台沙箱环境 appKey
    private static String appKey = "10001672";
    // 百望云沙箱环境 用户名
    private static String userName = "admin_3000000526480";
    // 百望云沙箱环境 密码
    private static String password = "123qwe!@#";
    // 百望云沙箱环境 appSecret
    private static String appSecret = "a570e227-9ec0-4bff-bdc5-e0e7b7638e86";
    // 百望云沙箱环境 用户盐值
    private static String userSalt = "8668a67a9c5a4453948bfd098642ab4d";

    public static void main(String[] args) {

        try {
            //自定义请求唯一标识
            String requestId = UUID.randomUUID().toString();
            System.out.println("requestId = " + requestId);
            PasswordLoginConfig loginConfig = new PasswordLoginConfig();
            loginConfig.setUrl(url);
            loginConfig.setClientId(appKey);
            loginConfig.setClientSecret(appSecret);
            loginConfig.setUsername(userName);
            loginConfig.setPassword(password);
            loginConfig.setUserSalt(userSalt);
            BopLoginClient loginClient = new PasswordLoginClient(loginConfig);
            System.out.println("loginConfig = " + JSONObject.toJSONString(loginConfig));
            // 获取token, token相关说明请参考 文档中心 -> 用户授权说明
            BopLoginResponse loginResponse = loginClient.login();
            // 重载方法，可以传自定义请求唯一标识
            // BopLoginResponse loginResponse = loginClient.login(requestId);
            String token = loginResponse.getResponse().getAccessToken();

            System.out.printf("用户【%s】的获取的token是：%s\n", userName, token);
        } catch (BWOpenException e) {
            System.out.println(e.getCode());
            System.out.println(e.getMessage());
            System.out.println(e.getSubCode());
            System.out.println(e.getSubMessage());
        }
    }

}
